from flask import Flask, request, Response, send_from_directory
from agents import chat, init_thread

app = Flask(__name__, static_folder="static")


@app.route("/")
def index():
    return send_from_directory(app.static_folder, 'index.html')


@app.route("/chat", methods=["POST"])
def main():
    data = request.get_json()
    input_prompt = data.get("prompt", "你好")
    thread_id = data.get("sessionId", "")
    if thread_id is None:
        response = init_thread()
        print(response)
        thread_id = response['thread_id']

    response = Response(
        chat(input_prompt=input_prompt, thread_id=thread_id),
        mimetype='text/event-stream'
    )
    return response


# 运行 web 程序
app.run(host="0.0.0.0", port=9000)
